package cn.didi.feidi.androidorder.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import cn.didi.feidi.androidorder.activity.FeidiBaseActivity;
import cn.didi.feidi.androidorder.assembly.FeidiActionBar;
import cn.didi.feidi.androidorder.core.AppActionRequest;
import cn.didi.feidi.androidorder.core.FeidiMessageHandler;
import cn.didi.feidi.androidorder.listener.OnFeidiFragmentNotifySessionListener;


/**
 * 继承基类着需提供：newInstance()方法
 * <p>
 * Created by king on 2017/7/17.
 */
public abstract class FeidiBaseFragment extends Fragment implements OnFeidiFragmentNotifySessionListener {

    protected FeidiBaseActivity mActivity;
    private long curClickTimeMillis = 0;

    ////  : 2017/7/17 使用该形式传递数据
//    public static XXXFragment newInstance(Bundle bundle) {
//        FeidiBaseFragment fragment = new XXXFragment();
//        fragment.setArguments(bundle);
//        return fragment;
//    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getActivity() instanceof FeidiBaseActivity) {
            mActivity = (FeidiBaseActivity) getActivity();
            mActivity.addOnFragmentNotifySessionListener(this);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        return init(initFragment(inflater, container, savedInstanceState));
    }

    private View init(View view) {
        initViews(view);
        initData();
        addListener();
        return view;
    }

    protected abstract View initFragment(LayoutInflater inflater, ViewGroup container,
                                         Bundle savedInstanceState);

    protected FeidiActionBar getFeidiActionBar() {
        return mActivity == null ? null : mActivity.getFeidiActionBar();
    }

    protected FeidiMessageHandler getFeidiHandler() {
        return mActivity == null ? null : mActivity.getFeidiMessageHandler();
    }

    protected void executeRequest(AppActionRequest request) {
        if (mActivity != null) {
            mActivity.executeRequest(request);
        }
    }

    protected void executeRequest(int msgId, String obj) {
        if (mActivity != null) {
            mActivity.executeRequest(msgId, obj);
        }
    }

    protected void makeToast(String text) {
        if (mActivity != null) {
            mActivity.makeToast(text);
        }
    }

    /**
     * 初始化组件
     */
    protected abstract void initViews(View rootView);

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 添加监听器
     */
    protected abstract void addListener();

    /**
     * 是否点击了多次 小于1秒当作是一次点击了
     **/
    protected boolean isMultiplicationClick() {
        if (curClickTimeMillis == 0) {
            curClickTimeMillis = System.currentTimeMillis();
            return false;
        }
        boolean rs = System.currentTimeMillis() - curClickTimeMillis < 1000;
        curClickTimeMillis = System.currentTimeMillis();
        return rs;
    }

    protected boolean isNull(String value) {
        return value == null || "".equals(value.trim());
    }

    @Override
    public void onSessionMessage(int msgId, Object object) {

    }

    @Override
    public void onSessionMessageException(int msgId, Exception exception) {

    }

    @Override
    public void onSessionClosed() {

    }

    @Override
    public void onSessionConnect() {

    }

    @Override
    public void onSessionTimeout() {

    }
}